package com.ruoyi.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.PurchaseRecord;
import com.ruoyi.purchase.service.IPurchaseRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author h w
 * @date 2022-12-10
 */
@RestController
@RequestMapping("/purchase/purchase")
public class PurchaseRecordController extends BaseController
{
    @Autowired
    private IPurchaseRecordService purchaseRecordService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(PurchaseRecord purchaseRecord)
    {
        startPage();
        List<PurchaseRecord> list = purchaseRecordService.selectPurchaseRecordList(purchaseRecord);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public List<PurchaseRecord> listAll(PurchaseRecord purchaseRecord)
    {
        return purchaseRecordService.selectPurchaseRecordList(purchaseRecord);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchase:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseRecord purchaseRecord)
    {
        List<PurchaseRecord> list = purchaseRecordService.selectPurchaseRecordList(purchaseRecord);
        ExcelUtil<PurchaseRecord> util = new ExcelUtil<PurchaseRecord>(PurchaseRecord.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchase:query')")
    @GetMapping(value = "/{purchaseRecordId}")
    public AjaxResult getInfo(@PathVariable("purchaseRecordId") Long purchaseRecordId)
    {
        return success(purchaseRecordService.selectPurchaseRecordByPurchaseRecordId(purchaseRecordId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchase:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseRecord purchaseRecord)
    {
        purchaseRecord.setCreateBy(getLoginUser().getUsername());
        int res = purchaseRecordService.insertPurchaseRecord(purchaseRecord);
        if(res == 0) {
            return error("采购单位在供应单位的余额不足，不能新建采购单！");
        }
        return toAjax(res);
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchase:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PurchaseRecord purchaseRecord)
    {
        return toAjax(purchaseRecordService.updatePurchaseRecord(purchaseRecord));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchase:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseRecordIds}")
    public AjaxResult remove(@PathVariable Long[] purchaseRecordIds)
    {
        return toAjax(purchaseRecordService.deletePurchaseRecordByPurchaseRecordIds(purchaseRecordIds));
    }
}
